class UsersController < ApplicationController
  before_filter :get_logged_user,
                :except => [:new, :create]
  before_filter :only_teachers,
                :except => [:new, :create, :edit, :update, :groups]
  before_filter :get_user
  before_filter :self_or_teacher,
                :only => [:edit, :update, :groups]
  # Shows projects list
  #
  def index
    @users = User.find(:all)
    
    respond_to do |wants|
      wants.html
      wants.xml   { render :xml => @users.to_xml }
    end
  end
  
  # Shows a single project
  #
  def show
    respond_to do |wants|
      wants.html
      wants.xml { render :xml => @user.to_xml }
    end
  end
  
  # Shows form to edit a project. Only for GET requests.
  #
  def edit
    
  end
  
  # Updates project
  #
  def update
    @user.is_teacher = params[:user][:is_teacher] if @logged_user.is_teacher?
    if @user.update_attributes(params[:user])
      respond_to do |wants|
        wants.html  { redirect_to user_url(@user) }
        wants.js
        wants.xml   { head :ok }
      end
    else
      respond_to do |wants|
        wants.html do
          flash.now[:error] = 'Unable to save details'
          render :action => "edit"
        end
        wants.js    { render :nothing => true, :status => 500 }
        wants.xml   { render :xml => @user.errors.to_xml }
      end
    end
  end
  
  # Shows form to create a new project. Only for GET requests.
  #
  def new
    @user = User.new
  end
  
  # Creates new project
  #
  def create
    @user = User.new(params[:user])
    
    if @user.save
      respond_to do |wants|
        wants.html  { redirect_to projects_path }
        wants.xml   { head :created, :location => user_url(@user) }
      end
    else
      respond_to do |wants|
        wants.html do
          flash.now[:error] = 'Unable to create account'
          render :action => "new"
        end
        wants.js    { render :nothing => true, :status => 500 }
        wants.xml   { render :xml => @user.errors.to_xml }
      end
    end
  end
  
  # Deletes project
  #
  def destroy
    @user.destroy
    
    respond_to do |wants|
      wants.html do
        flash[:note] = 'User deleted'
        redirect_to users_url
      end
      wants.js  { head :ok }
      wants.xml { head :ok }
    end
  end
  
  # Shows classes belonging to @user
  #
  def classes
    @teachings = @user.teachings.find(:all)
    
    respond_to do |wants|
      wants.html
      wants.xml   { render :xml => @teachings.to_xml }
    end
  end
  
  # Shows groups belonging to @user
  #
  def groups
    @groups = @user.groups.find(:all, :include => {:teaching => :project})
    
    respond_to do |wants|
      wants.html
      wants.xml   { render :xml => @groups.to_xml }
    end
  end
private
  # If params[:id] is set, retrieves @user
  #
  def get_user
    unless params[:id].nil? || params[:id].blank?
      @user = User.find(params[:id])
      raise if @user.nil?
    end
    return true
  rescue
    respond_to do |wants|
      wants.html { render :text => "The user you're looking for doesn't exist!", :layout => true, :status => 404 }
      wants.js   { render :nothing => true, :status => 404 }
      wants.xml  { head :not_found }
    end
    return false
  end
  
  # Only teachers or self should be allowed in
  #
  def self_or_teacher
    unless(@logged_user.is_teacher? || (@user.id == @logged_user.id))
      respond_to do |wants|
        wants.html { render :text => "The user you're looking for doesn't exist!", :layout => true, :status => 401 }
        wants.js   { render :nothing => true, :status => 404 }
        wants.xml  { head :not_found }
      end
      return false
    end
    return true
  end
end
